$(document).ready(function(){
	var checkedAll=$(".check-all");
	var checkedOne=$(".check-one");
	var rows =$("#tbody tr");
	var subAll=$("#priceTotal");
	var countAll=$("#selectedTotal");
	var delAll=$("#deleteAll");
	var count="";
	var del="";
	var price="";
	var sub="";
	//全选
	 $(".check-all").on('change', function() {
		if (this.checked) {
			for(var i=0;i<checkedOne.length;i++){
				checkedOne[i].checked="checked";
				checkedAll[0].checked="checked";
				checkedAll[1].checked="checked";
			}
			subAllF();
		} else {
		   for(var i=0;i<checkedOne.length;i++){
			checkedOne[i].checked="";
			checkedAll[0].checked="";
			checkedAll[1].checked="";
		   }
		   subAllF();
		}
		
	}); 

	//总计
	function subAllF(){
			rows = $("#tbody tr");
			var subAlls=0;
			var countAlls=0;
			rows.each(function(i,e){
				var sub=$(this).find(".subtotal").text();
				var count=$(this).find(".count-input").val();
				if(checkedOne[i].checked || checkedAll[0].checked || checkedAll[1].checked){
					subAlls=(parseFloat(subAlls)+parseFloat(sub)).toFixed(2);
					countAlls=Number(countAlls)+Number(count);
				}	
			});
			subAll.html(parseFloat(subAlls));
			countAll.html(Number(countAlls));
		}
	//获取表单信息
	rows.each(function(i,el){
        count=$(this).find(".count-input");
        del=$(this).find(".reduce");
        price=$(this).find(".price");
        sub=$(this).find(".subtotal");
        //添加数据
		$(this).click(function(e){
			//增减数量，删除
			if($(e.target).attr("class")=="add"){
				del.html("-");
				count.val(Number(count.val())+1);
				sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
			}else if($(e.target).attr("class")=="reduce"){
				if(count.val()>1){
					count.val(count.val()-1);
				}else{
					count.val(0);
					del.html("");
				}
				sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
			}else if($(e.target).attr("class")=="check-one check"){
				checkedAll[0].checked="";
				checkedAll[1].checked="";
			}/*else if($(e.target).attr("class")=="delete"){
				var del=confirm("确定要删除？");
				if(del){
                    window.location.href = '?delete_id=' + id;
				}
			
			}*/
			subAllF();
		});

		//键盘选取
		$(this).find(".count-input").on("keyup",function(e){
			count=$(this).find(".count-input");
			del=$(this).find(".reduce");
			price=$(this).find(".price");
			sub=$(this).find(".subtotal"); 
			sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
			
			if(Number(count.val())>1){
				del.html("-");
			}else{
				del.html("");
			}
			subAllF();
		});
		//减号显示
        if(count.val()>0){
            del.html("-");
        }
	});


	//全部删除
	delAll.click(function(){
        if(checkedAll[0].checked || checkedAll[1].checked){
            var del=confirm("确定要全部删除吗");
            if(del) {
                window.location.href = '?del_id=0';
            }
        }
	}); 
});
//删除
function delF(id){
    var del=confirm("确定要删除？");
    if(del && id!=""){
        window.location.href = '?del_id=' + id;
    }
}
//操作
function cF(pId,uId,num){
    if(pId!="" && uId!="" && num>=0){
        window.location.href = '?pId=' + pId+'&uId='+uId+'&num='+num;
    }
}

$(document).ready(function(){
    var checkedAll=$(".check-all");
    var checkedOne=$(".check-one");
    var rows =$("#tbody tr");
    var subAll=$("#priceTotal");
    var countAll=$("#selectedTotal");
    var delAll=$("#deleteAll");alert(1);
    var count="";
    var del="";
    var price="";
    var sub="";
    //全选
    $(".check-all").on('change', function() {
        if (this.checked) {
            for(var i=0;i<checkedOne.length;i++){
                checkedOne[i].checked="checked";
                checkedAll[0].checked="checked";
                checkedAll[1].checked="checked";
            }
            subAllF();
        } else {
            for(var i=0;i<checkedOne.length;i++){
                checkedOne[i].checked="";
                checkedAll[0].checked="";
                checkedAll[1].checked="";
            }
            subAllF();
        }

    });

    //总计
    function subAllF(){
        rows = $("#tbody tr");
        var subAlls=0;
        var countAlls=0;
        rows.each(function(i,e){
            var sub=$(this).find(".subtotal").text();
            var count=$(this).find(".count-input").val();
            if(checkedOne[i].checked || checkedAll[0].checked || checkedAll[1].checked){
                subAlls=(parseFloat(subAlls)+parseFloat(sub)).toFixed(2);
                countAlls=Number(countAlls)+Number(count);
            }
        });
        subAll.html(parseFloat(subAlls));
        countAll.html(Number(countAlls));
    }
    //获取表单信息
    rows.each(function(i,el){
        count=$(this).find(".count-input");
        del=$(this).find(".reduce");
        price=$(this).find(".price");
        sub=$(this).find(".subtotal");
        //添加数据
        $(this).click(function(e){
            //增减数量，删除
            if($(e.target).attr("class")=="add"){
                del.html("-");
                count.val(Number(count.val())+1);
                sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
            }else if($(e.target).attr("class")=="reduce"){
                if(count.val()>1){
                    count.val(count.val()-1);
                }else{
                    count.val(0);
                    del.html("");
                }
                sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));
            }else if($(e.target).attr("class")=="check-one check"){
                checkedAll[0].checked="";
                checkedAll[1].checked="";
            }/*else if($(e.target).attr("class")=="delete"){
			 var del=confirm("确定要删除？");
			 if(del){
			 window.location.href = '?delete_id=' + id;
			 }

			 }*/
            subAllF();
        });

        //键盘选取
        $(this).find(".count-input").on("keyup",function(e){
            count=$(this).find(".count-input");
            del=$(this).find(".reduce");
            price=$(this).find(".price");
            sub=$(this).find(".subtotal");
            sub.html((parseFloat(price.text())*Number(count.val())).toFixed(2));

            if(Number(count.val())>1){
                del.html("-");
            }else{
                del.html("");
            }
            subAllF();
        });
        //减号显示
        if(count.val()>0){
            del.html("-");
        }
    });

    alert(1);
    //全部删除
    $('#d').click(function () {
        alert(1);
    });
    delAll.click(function(){alert(1);
        if(checkedAll[0].checked || checkedAll[1].checked){
            var del=confirm("确定要全部删除吗");
            if(del) {
                window.location.href = '?del_id=0';
            }
        }
    });
});
//删除
function delF(id){
    var del=confirm("确定要删除？");
    if(del && id!=""){
        window.location.href = '?del_id=' + id;
    }
}
//操作
function cF(pId,uId,num){
    if(pId!="" && uId!="" && num>=0){
        window.location.href = '?pId=' + pId+'&uId='+uId+'&num='+num;
    }
}

